package com.zxy.ziems.server.alarm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.alarm.convertor.AlarmConvertor;
import com.zxy.ziems.server.alarm.mapper.AlarmRuleRoleMapper;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmRuleRoleEntity;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmRuleRoleVO;
import com.zxy.ziems.server.alarm.service.AlarmRuleRoleService;
import com.zxy.ziems.server.domain.Role;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Copyright (C),
 * @ClassName: AlarmRuleRoleServiceImpl
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/4/10 15:42
 */
@Service
@Slf4j
public class AlarmRuleRoleServiceImpl extends ServiceImplX<AlarmRuleRoleMapper, AlarmRuleRoleEntity> implements AlarmRuleRoleService {

    /**
     * 保存告警通知角色
     *
     * @param ruleId
     * @param roleList
     */
    @Override
    public void saveOrUpdateAlarmRuleRole(String ruleId, List<String> roleList) {
        deleteAlarmRuleRoleByRuleId(ruleId);

        if (roleList == null) {
            return;
        }

        List<AlarmRuleRoleEntity> entityList = new ArrayList<>();
        for (String roleId : roleList) {
            AlarmRuleRoleEntity entity = new AlarmRuleRoleEntity();
            entity.setRuleId(ruleId);
            entity.setRoleId(roleId);
            entityList.add(entity);
        }
        this.saveBatch(entityList);
    }

    @Override
    public void deleteAlarmRuleRoleByRuleId(String ruleId) {
        QueryWrapper<AlarmRuleRoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AlarmRuleRoleEntity::getRuleId, ruleId);
        this.remove(queryWrapper);
    }

    @Override
    public List<AlarmRuleRoleVO> listByIdList(Set<String> ruleIdSet) {
        if(CollectionUtil.isEmpty(ruleIdSet)){
            return new ArrayList<>();
        }
        QueryWrapper<AlarmRuleRoleEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().in(AlarmRuleRoleEntity::getRuleId, ruleIdSet);
        List<AlarmRuleRoleEntity> list = this.list(queryWrapper);
        Set<String> roleIdSet = new HashSet<>();
        for (AlarmRuleRoleEntity entity : list) {
            if (StrUtil.isNotBlank(entity.getRoleId())) {
                roleIdSet.add(entity.getRoleId());
            }
        }

        Map<String, Role> roleMap = roleService.listByIds(roleIdSet).stream()
                .collect(Collectors.toMap(Role::getRoleId, (a) -> a));

        List<AlarmRuleRoleVO> alarmRuleRoleVOList = list.stream()
                .map((entity) -> {
                    Role role = StrUtil.isBlank(entity.getRoleId()) ? null : roleMap.get(entity.getRoleId());

                    return AlarmConvertor.INSTANCE.alarmRuleRoleDOConvertor2VO(
                            entity,
                            role);
                }).toList();

        return alarmRuleRoleVOList;
    }

    @Override
    public List<AlarmRuleRoleEntity> listByRuleId(String ruleId) {
        LambdaQueryWrapper<AlarmRuleRoleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlarmRuleRoleEntity::getRuleId, ruleId);
        return baseMapper.selectList(wrapper);
    }


    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

}
